﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using LittleBee.CellStore;

namespace LittleBee.CellStore.UserControls
{
	public partial class UCCellProperty : UserControl
	{
		public UCCellProperty()
		{
			InitializeComponent();
		}

		private void UCCellProperty_Load(object sender, EventArgs e)
		{
			StoreCell cell = (Tag as StoreCell)!;

			tbCellType.Text = cell.CellType;
			tbCellName.Text = cell.CellName;
			tbCellDescription.Text = cell.CellDescription;

			BindingSource bs = new BindingSource();
			bs.DataSource = FormMain.CellClasses;
			//cbCellBaseType.SelectedIndex = GetBaseClassIndex(cell.BaseClass);
			cbCellBaseType.DataSource = bs;
			cbCellBaseType.DisplayMember = "Key";
			cbCellBaseType.ValueMember = "Value";

			cbCellBaseType.Tag = cell.BaseClass;
			cbCellBaseType.SelectedIndex = GetBaseClassIndex(cell.BaseClass);
		}

		private int GetBaseClassIndex(string baseClass)
		{
			int i = 0;
			foreach (var item in FormMain.CellClasses)
			{
				if (item.Key == baseClass)
					return i;
				++i;
			}

			return -1;
		}

		private void tbCellType_Leave(object sender, EventArgs e)
		{
			StoreCell cell = (Tag as StoreCell)!;

			cell.CellType = tbCellType.Text;
		}

		private void tbCellName_Leave(object sender, EventArgs e)
		{
			StoreCell cell = (Tag as StoreCell)!;

			cell.CellName = tbCellName.Text;
		}

		private void tbCellDescription_Leave(object sender, EventArgs e)
		{
			StoreCell cell = (Tag as StoreCell)!;

			cell.CellDescription = tbCellDescription.Text;
		}

		private void cbCellBaseType_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (cbCellBaseType.Tag == null)
				return;
			if (cbCellBaseType.SelectedIndex < 0)
				return;

			StoreCell cell = (Tag as StoreCell)!;

			cell.BaseClass = cbCellBaseType.Text;
			cbCellBaseType.Tag = cell.BaseClass;
		}
	}
}
